package com.ljj;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-11-17
 * Time: 22:15
 */
@RestController
@RequestMapping("/request")
public class RequestController {
    @RequestMapping("/r1")
    public String r1(String s1){
        return "接受到参数:" + s1;
    }
    @RequestMapping("/r2")
    public String r2(Integer age){
        if(age == null){
            return "参数不能为空";
        }
        return "接受到参数" + age;
    }
    @RequestMapping("/r3")
    public String r3(String name, Integer age){
        return String.format("接受到参数:姓名[%s], age[%d]", name, age);
    }
    @RequestMapping("/r4")
    public String r4(Person person){
        return "接受到参数" + person;
    }
    //不加RequestParam重命名后端参数，那么这个name就不是必须要传递
    @RequestMapping("/r5")
    public String r5(String name){
        return  "接受到参数" + name;
    }
    //加了RequestParam重命名后端参数并且传递前端参数的名字，就必须要传数name
    @RequestMapping("/r6")
    public String r6(@RequestParam("sa") String name){
        return "接受到参数" + name;
    }
    //其实也不是必须要传，设置required = false 就可以不必须传输了
    @RequestMapping("/r7")
    public String r7(@RequestParam(value = "sa", required = false) String name){
        return "接受到参数" + name;
    }
    @RequestMapping("/r8")
    public String r8(String[] name){
        return "接受到参数" + Arrays.toString(name);
    }
    @RequestMapping("/r9")
    //前端传参方式和传数组参数一样，后端这里使用RequstParam重命名是让他从数组(不能构造)，改绑定到集合List可以构造
    public String r9(@RequestParam List<String> list) {
        return "接受到参数: " + list;
    }
    @RequestMapping("/r10")
    public String r10(@RequestBody Person person){
        return "接收到参数" + person;
    }
    @RequestMapping("/r11/{articleId}")
    public String r11(@PathVariable("articleId") Integer arId){
        return "接收到参数" + arId;
    }
    //和PathVariable绑定新名称，如果参数名和后端参数名不一样Pathvariable不需要写参数
    @RequestMapping("/r12/{type}/{UserName}")
    public String r12(@PathVariable String type, @PathVariable("UserName") String name){
        return "接受到参数" + type + " " + name;
    }
    //上传文件
    @RequestMapping("/r13")
    public String r13(MultipartFile file) throws IOException {
        //这里是获取这里的文件变量名字
        String name = file.getName();
        //获取真实文件名字
        String originalFilename = file.getOriginalFilename();
        System.out.println("originalFilename:" + originalFilename);
        String contentType = file.getContentType();
        //文件上传
        file.transferTo(new File("D:\\可清理\\" + originalFilename));
        return "接受到文件" + name;
    }


}
